import ctypes
import sys
import http.client
import time
import vlc
# from vlc import VideoMarqueeOption, Position, EventType,Instance

class RTSP_Client():
    pass


class VLC_Player():

    def __init__(self, url):
        self.url = url

    def start_with_marquee(self,timeout=60):


        movie = self.url
        # Need --sub-source=marq in order to use marquee below
        print(sys.argv[:])
        instance =vlc.Instance(["--sub-source=marq"] + sys.argv[1:])
        try:
            media = instance.media_new(movie)
        except (AttributeError, NameError) as e:
            sys.exit(1)
        player = instance.media_player_new()
        player.set_media(media)
        player.play()

        # Some marquee examples.  Marquee requires '--sub-source marq' in the
        # Instance() call above, see <http://www.videolan.org/doc/play-howto/en/ch04.html>
        player.video_set_marquee_int(vlc.VideoMarqueeOption.Enable, 1)
        player.video_set_marquee_int(vlc.VideoMarqueeOption.Size, 24)  # pixels
        player.video_set_marquee_int(vlc.VideoMarqueeOption.Position, vlc.Position.Bottom)
        player.video_set_marquee_int(vlc.VideoMarqueeOption.Timeout, 0)  # millisec, 0==forever
        player.video_set_marquee_int(vlc.VideoMarqueeOption.Refresh, 1000)  # millisec (or sec?)
        ##t = '$L / $D or $P at $T'
        t = '%Y-%m-%d  %H:%M:%S'
        player.video_set_marquee_string(vlc.VideoMarqueeOption.Text, str_to_bytes(t))

        # Some event manager examples.  Note, the callback can be any Python
        # callable and does not need to be decorated.  Optionally, specify
        # any number of positional and/or keyword arguments to be passed
        # to the callback (in addition to the first one, an Event instance).
        event_manager = player.event_manager()
        event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, end_callback)
        event_manager.event_attach(vlc.EventType.MediaPlayerPositionChanged, pos_callback, player)
        time.sleep(timeout)

    def start(self,timeout=60):

        instance = vlc.Instance()
        player = instance.media_player_new()
        Media = instance.media_new(self.url)
        Media.get_mrl()
        player.set_media(Media)
        player.play()
        time.sleep(timeout)


def str_to_bytes(s):
    """Translate string or bytes to bytes.
    """
    if isinstance(s, str):
        return bytes(s, encoding="UTF-8")
    else:
        return s

def end_callback(event):
    print('End of media stream (event %s)' % event.type)
    sys.exit(0)

echo_position = False
def pos_callback(event, player):
    if echo_position:
        sys.stdout.write('\r%s to %.2f%% (%.2f%%)' % (event.type,
                            event.u.new_position * 100,
                            player.get_position() * 100))
        sys.stdout.flush()

if __name__ == "__main__":
    url = "rtsp://192.168.1.173:8554/live1.264"
    p = VLC_Player(url)
    p.start(6000)
